﻿Imports System.Net
Public Class DownloadProject
    Private WithEvents httpclient As WebClient
    Dim path = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\Gadget Creator\project.gcp"
    Private Sub httpclient_DownloadFileCompleted(sender As Object, e As System.ComponentModel.AsyncCompletedEventArgs) Handles httpclient.DownloadFileCompleted
        Dim tmpdir As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\Gadget Creator\tmp\"
        Try
            Using zip1 As Ionic.Zip.ZipFile = Ionic.Zip.ZipFile.Read(path)
                Dim y As Ionic.Zip.ZipEntry
                For Each y In zip1
                    y.Extract(tmpdir, Ionic.Zip.ExtractExistingFileAction.OverwriteSilently)
                Next
            End Using
            System.IO.File.Delete(path)
        Catch ex As Exception
            MsgBox("Couldn't open project. Maybe its broken: " + ex.Message)
            System.IO.Directory.Delete(tmpdir, True)
            Exit Sub
        End Try
        Try
            If System.IO.File.Exists(tmpdir + "gadget.xml") = False Then
                MsgBox("Project is broken! It's cannot be loaded!")
                Me.Close()
                Exit Sub
            End If
            GeneralForm.tabcontrol.TabPages.Clear()
            If System.IO.File.Exists(tmpdir + "gadget.html") = False Then
                If MsgBox("Gadget's main page was not found. Do you want Gadget Creator to create new?", 36, "Loading project...") = MsgBoxResult.Yes Then
                    Dim gadgettab As New TabPage
                    gadgettab.Name = "gadget.html"
                    gadgettab.Text = "Gadget"
                    Dim htmlbox As New FastColoredTextBoxNS.FastColoredTextBox
                    htmlbox.Language = FastColoredTextBoxNS.Language.HTML
                    If My.Settings.linenumbers = True Then
                        htmlbox.ShowLineNumbers = True
                    Else
                        htmlbox.ShowLineNumbers = False
                    End If
                    Dim writer As New System.IO.StreamWriter(tmpdir + "gadget.html", True, System.Text.Encoding.GetEncoding(1251))
                    writer.Write(My.Settings.gadgetsyntax)
                    writer.Close()
                    htmlbox.Text = My.Settings.gadgetsyntax
                    htmlbox.Dock = DockStyle.Fill
                    gadgettab.Controls.Add(htmlbox)
                    GeneralForm.tabcontrol.TabPages.Add(gadgettab)
                    GeneralForm.showall()
                    Me.Close()
                    GeneralForm.browsefiles.Visible = True
                    GeneralForm.refreshfiles()
                Else
                    GeneralForm.showall()
                    GeneralForm.browsefiles.Visible = True
                    GeneralForm.refreshfiles()
                    Me.Close()
                End If
            Else
                Dim gadgettab As New TabPage
                gadgettab.Name = "gadget.html"
                gadgettab.Text = "Gadget"
                Dim htmlbox As New FastColoredTextBoxNS.FastColoredTextBox
                htmlbox.Language = FastColoredTextBoxNS.Language.HTML
                If My.Settings.linenumbers = True Then
                    htmlbox.ShowLineNumbers = True
                Else
                    htmlbox.ShowLineNumbers = False
                End If
                Dim reader As New System.IO.StreamReader(tmpdir + "gadget.html", System.Text.Encoding.GetEncoding(1251))
                htmlbox.Text = reader.ReadToEnd()
                reader.Close()
                htmlbox.Dock = DockStyle.Fill
                gadgettab.Controls.Add(htmlbox)
                GeneralForm.tabcontrol.TabPages.Add(gadgettab)
                GeneralForm.showall()
                GeneralForm.browsefiles.Visible = True
                GeneralForm.refreshfiles()
                Me.Close()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub dpc(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles httpclient.DownloadProgressChanged
        If httpclient.IsBusy = True Then
            progress.Value = e.ProgressPercentage
        Else
            progress.Style = ProgressBarStyle.Marquee
        End If
    End Sub

    Private Sub DownloadProject_Shown(sender As System.Object, e As System.EventArgs) Handles MyBase.Shown
        Try
            Dim tmpdir As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\Gadget Creator\tmp\"
            If System.IO.Directory.Exists(tmpdir) = True Then
                My.Computer.FileSystem.DeleteDirectory(tmpdir, FileIO.DeleteDirectoryOption.DeleteAllContents)
                My.Computer.FileSystem.CreateDirectory(tmpdir)
            Else
                My.Computer.FileSystem.CreateDirectory(tmpdir)
            End If
            httpclient = New WebClient
            httpclient.DownloadFileAsync(New Uri(Me.Tag.ToString), (path))
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class